package handler

import (
	"common"
	"common/biz"
	"common/logs"
	"context"
	"core/repo"
	"core/service"
	"encoding/json"
	"framework/remote"
	"hall/models/request"
	"hall/models/response"
	"strconv"
)

type NewsHandler struct {
	newsService *service.NewsService
	base        *BaseHandler
}

// Notice 公告
func (n *NewsHandler) Notice(session *remote.Session, msg []byte) any {
	logs.Info("[newsHandler] Notice msg:%v", string(msg))
	data := n.newsService.NoticeAll(context.TODO())
	return common.S(data)
}

// GetNotice 公告详情
func (n *NewsHandler) GetNotice(session *remote.Session, msg []byte) any {
	logs.Info("[newsHandler] GetNotice msg:%v", string(msg))
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[newsHandler] GetNotice err:缺少参数 req=%v,err=%v", req, err)
		return common.F(biz.RequestDataError)
	}
	data := n.newsService.GetNotice(context.TODO(), req.ID)
	return common.S(data)
}

// EmailList 邮箱列表
func (n *NewsHandler) EmailList(session *remote.Session, msg []byte) any {
	logs.Info("[newsHandler] EmailList msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[newsHandler] EmailList err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := n.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.PageMsg
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[newsHandler]用户%d EmailList err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	count, data := n.newsService.EmailList(context.TODO(), uint(uid), &req)
	return common.S(response.CommonList{
		Count: count, List: data,
	})
}

// GetEmail 邮箱详情
func (n *NewsHandler) GetEmail(session *remote.Session, msg []byte) any {
	logs.Info("[newsHandler] GetEmail msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[newsHandler] GetEmail err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := n.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[newsHandler]用户%d GetEmail err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	data, errs := n.newsService.GetEmail(context.TODO(), uint(uid), req.ID)
	if errs != nil {
		return common.F(errs)
	}
	return common.S(data)
}

// EmailReceive 领取
func (n *NewsHandler) EmailReceive(session *remote.Session, msg []byte) any {
	logs.Info("[newsHandler] EmailReceive msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[newsHandler] EmailReceive err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := n.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[newsHandler]用户%d EmailReceive err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	errs := n.newsService.EmailReceive(context.TODO(), uint(uid), req.ID)
	if errs != nil {
		return common.F(errs)
	}
	return common.S(nil)
}

// EmailRemove 删除
func (n *NewsHandler) EmailRemove(session *remote.Session, msg []byte) any {
	logs.Info("[newsHandler] EmailRemove msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[newsHandler] EmailRemove err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := n.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.NewsRemove
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[newsHandler]用户%d EmailRemove err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	errs := n.newsService.EmailRemove(context.TODO(), uint(uid), &req)
	if errs != nil {
		return common.F(errs)
	}
	return common.S(nil)
}

// Record 日志记录
func (n *NewsHandler) Record(session *remote.Session, msg []byte) any {
	logs.Info("[newsHandler] Record msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[newsHandler] Record err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := n.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.Record
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[newsHandler]用户%d Record err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	count, data := n.newsService.Record(context.TODO(), uint(uid), &req)
	return common.S(response.CommonList{
		Count: count, List: data,
	})
}

func NewNewsHandler(r *repo.Manager) *NewsHandler {
	return &NewsHandler{
		newsService: service.NewNewsService(r),
		base:        NewBaseHandler(r),
	}
}
